package com.itheima.stock.config;


import com.itheima.stock.common.domain.StockInfoConfig;
import com.itheima.stock.utils.IdWorker;
import com.itheima.stock.utils.ParserStockInfoUtil;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;

//定义公共配置bean
@Configuration
@EnableConfigurationProperties(StockInfoConfig.class)
public class CommonConfig {

    //定义密码加密和解密器
    @Bean
    public PasswordEncoder passwordEncoder(){
        return new BCryptPasswordEncoder();
    }

    //配置id生成器 保证分布式环境下的id唯一
    @Bean
    public IdWorker idWorker() {
        //参数1 是指定机器的id序列号
        //参数2 是指定机房的id序列号
        IdWorker idWorker = new IdWorker(1L,2L);
        return idWorker;
    }

    @Bean
    public ParserStockInfoUtil parserStockInfoUtil(IdWorker idWorker) {
        ParserStockInfoUtil stockInfoUtil = new ParserStockInfoUtil(idWorker);
        return stockInfoUtil;

    }

}
